import {ref} from 'vue' 

// 评论、留言共用模块方法
let refs = null; //保存滚动容器
let isLoding = true; //判定评论拉取状态，true表示结束，false表示还未结束，用来加锁的
let index_skip = 1; //懒加载分页页数
let route_params__id =''; // 评论模块保存BlogId
let limit = 10; //每页多少条评论
export const commentList = ref([]); // 留言或评论列表 模块销毁时需要清空
export const count = ref(0); // 评论或留言总数 模块销毁时需要清空
export const commentListTemp = ref([]);

export const isShowMessge = ref(false);
export const isClear = ref(false);
export const rIs = ref(false); //提交评论，返回结果判定，true表示成功，默认失败
export const resultErr = ref('');// 提交评论，返回错误信息


export function getRef(){
    return refs
};

export function setRef(value){ 
    refs = value;
}

export function setParmsId(id){

    route_params__id = id;
}

export  async function get_Comment(fn,skip =1,limit=10){ //获取评论
    isLoding = false;
    const result = await fn(limit,skip,route_params__id);
    commentList.value.push(...result.row);
    count.value = result.count;
    index_skip++;
   
    isLoding = true;
  }
  export function setScrollBox(str){

    setRef(document.querySelector(str));
}

export async function loading(fn){
    if(Math.abs(getRef().scrollTop + getRef().clientHeight -getRef().scrollHeight) <= 100 && isLoding ){
        if(commentList.value.length < count.value && commentListTemp.value.length + commentList.value.length < count.value){
            
            await get_Comment(fn,index_skip,limit,route_params__id)
        }
    }  
}

export async function post_Comment(fn,name,content,id,text,title = ''){//提交评论
    console.log('useRef',title)
    const result = await fn(name,content,id,text,route_params__id,title);
            
           if(result instanceof Object){
              
               commentListTemp.value.push(result);
              
               isShowMessge.value = false;
               isClear.value = true;
               rIs.value = false;
               resultErr.value = '';
               count.value ++;
           }else{
               rIs.value = true;
               resultErr.value = result;
           }
}

export  function delData(){

    index_skip = 1;
    commentList.value = [];
    count.value = 0;
    route_params__id = '';
    resultErr.value = ""
    refs = '';
    rIs.value = false;
    commentListTemp.value =[];

}